package com.wlw.base;

import com.alibaba.fastjson.JSONObject;
import com.wlw.exception.AppUserException;
import com.wlw.service.RedisServiceImpl;
import com.wlw.tools.AesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * 基类
 */
@Slf4j
public class Base {
    public       String appKey = "cpj2xarlchgan";
    public      String appSecret = "lLK4OsYwI8";
    @Autowired
    private RedisServiceImpl redisServiceImpl;
    public JSONObject getBaseUserInfo(String sessionId)throws AppUserException {
        String json=redisServiceImpl.getValue(sessionId);
        JSONObject object=JSONObject.parseObject(json);
        return object;
    }
    /**
     * 生成订单号
     * @return
     */
    public String getOid(){
            int machineId = 1;
            int hashCodeV = UUID.randomUUID().toString().hashCode();
            if (hashCodeV < 0) {
                hashCodeV = -hashCodeV;
            }
            SimpleDateFormat format0 = new SimpleDateFormat("yyMMdd");
            String danzi ="WLW"+format0.format(new Date()) + String.format("%05d", hashCodeV);return danzi;
    }
    /**
     * 解密数据
     * @return
     */
    public String getData(HttpServletRequest request) throws Exception {
        String data=request.getParameter("data");
        String relust= AesUtil.decrypt(data);//解密数据
        return relust;
    }
    /**
     * 解密数据
     * @return
     */
    public String getDataString(String data) throws Exception {
        String relust= AesUtil.decrypt(data);//解密数据
        return relust;
    }
    /**
     * 加密数据
     * @return
     */
    public String setData(String data)  {
        log.info("吧返回的数据加密");
        String relust=null;
        try{
             relust= AesUtil.encrypt(data);//解密数据
        }catch(Exception e){
       log.info("加密出错",e);
        }
        return relust;
    }

    /**
     * 生成6位验证码
     * @return
     */
    public  String randomCode() {
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            str.append(random.nextInt(10));
        }
        return str.toString();
    }
}
